
package com.yyfax.ph.home.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.yyfax.ph.R;
import com.yyfax.ph.common.utils.ListUtils;
import com.yyfax.ph.common.widgets.autoscrollviewpager.RecyclingPagerAdapter;

import java.util.ArrayList;
import java.util.List;


public class ImagePagerAdapter extends RecyclingPagerAdapter {

	private Context mContext;
	private List<BannerModel.BannerBean> mImageIdList;

	private int mSize;
	private boolean mIsInfiniteLoop;

	public void setPositionListener(PositionListener mListener) {
		this.mListener = mListener;
	}

	private PositionListener mListener;
	private RequestManager mRequestManager;

	public ImagePagerAdapter(Context ctx, List<BannerModel.BannerBean> list) {
		mContext = ctx;
		mImageIdList = list;
		mSize = mImageIdList.size();
		mIsInfiniteLoop = true;
		mRequestManager = Glide.with(ctx);
	}

	public ImagePagerAdapter(Context ctx) {
		this(ctx, new ArrayList<BannerModel.BannerBean>());
	}

	public void setImageList(List<BannerModel.BannerBean> list) {
		if (null != list && list.size() > 0) {
			mImageIdList.clear();
			mImageIdList.addAll(list);
			mSize = list.size();
			mIsInfiniteLoop = true;
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		if (mImageIdList.size() <= 0) {
			return 0;
		} else {
			return mIsInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(mImageIdList);
		}
	}

	private int getPosition(int position) {
		if (mSize == 0) {
			return 0;
		}
		return mIsInfiniteLoop ? position % mSize : position;
	}

	@Override
	public View getView(int position, View view, ViewGroup container) {
		ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.ad_img_item_layout, container, false);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final BannerModel.BannerBean bannerBean = mImageIdList.get(getPosition(position));
		mRequestManager.load(bannerBean.imageUrl).placeholder(R.drawable.bg_banner_placeholder).error(R.drawable.bg_banner_placeholder).centerCrop().into(holder.imageView);

		if (mListener != null) {
			holder.imageView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mListener.selectedAd(bannerBean);
				}
			});
		}
		return view;
	}

	public boolean isInfiniteLoop() {
		return mIsInfiniteLoop;
	}


	public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
		mIsInfiniteLoop = isInfiniteLoop;
		return this;
	}

	static class ViewHolder {
		@BindView(R.id.ad_img)
		ImageView imageView;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

	public interface PositionListener {
		void selectedAd(BannerModel.BannerBean bannerBean);
	}
}
